//
//  BKHttpRequestQueue.h
//  Pods
//
//  Created by baboy on 1/18/16.
//
//
#import <Foundation/Foundation.h>

#ifndef _BKHttpRequestTask_
#define _BKHttpRequestTask_

NS_ASSUME_NONNULL_BEGIN
@class BKHttpResponseHandler, BKHttpRequestQueue;


@interface BKHttpRequestTask: NSObject
@property(nonatomic, strong) NSString *taskId;
@property(nonatomic, strong) NSURLSessionTask *sessionTask;
@property(nonatomic, strong) NSMutableArray *responseHandlers;
@end

@interface BKHttpRequestPool : NSObject
@property (nonatomic, strong, readonly) NSDictionary * pool;
@property (nonatomic, strong) BKHttpRequestQueue *queue;

+ (id _Nonnull)defaultPool;
- (void)addSessionTask:(NSURLSessionTask *)sessionTask withResponseHandler:(BKHttpResponseHandler *)responseHandler;
- (void)addResponseHandler:(BKHttpResponseHandler *)responseHandler;
- (void)removeResponseHandler:(BKHttpResponseHandler *)responseHandler;

- (NSURLSessionTask *_Nullable)sessionTaskForTaskId:(NSString *)taskId;

- (void)clearTaskWithTaskId:(NSString *)taskId;
- (void)clearTaskWithTaskId:(NSString *)taskId runWithBlock:(nullable void(^)(BKHttpResponseHandler *_Nonnull)) block;
@end

@interface BKHttpResponseHandler: NSObject
@property (nonatomic, strong) NSString * taskId;
@property (nonatomic, strong) NSUUID * uuid;
@property (nonatomic, strong) NSURLSessionTask * sessionTask;
@property (nonatomic, copy) void (^_Nullable successBlock )(id _Nullable task, id _Nullable data);
@property (nonatomic, copy) void (^_Nullable failureBlock)(id _Nullable task, id _Nullable data, NSError* _Nullable error);
- (id _Nonnull)initWithTaskId:(NSString *)taskId UUID:(NSUUID *)uuid;
@end


@interface BKHttpRequestQueue : NSObject
@property (nonatomic, assign) int maxConcurrentTaskCount;
@property (nonatomic, assign) int activeTaskCount;

- (void)startNext; //启动任务，可多次调用
- (void)enqueue:(NSURLSessionTask *)task;
- (void)cancel:(NSURLSessionTask *)task;
- (void)sessionTaskFinished:(NSURLSessionTask * _Nullable)task;
@end
NS_ASSUME_NONNULL_END

#endif
